﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Boomer.Source.TiledMap;
using Boomer.Source.Character;
using FrameWork.Source.Global_variables;

namespace Boomer.Source.Boom
{
    class BoomManager
    {

        private static BoomManager manager = null;

        private List<CBoom> listBoom;

        private CTiledMap map;

        BoomManager()
        {
            listBoom = new List<CBoom>();
        }


        public static BoomManager getInstance()
        {
            if (manager == null)
                manager = new BoomManager();

            return manager;
        }

        public void createBoom(CCharacter user , bool isNormalBoom)
        {
            int index = isHasRoom(isNormalBoom);
            if (index == -1)
            {
                CBoom boom;
                if (isNormalBoom )
                    boom = new CBoom();
                else
                    boom = new CActiveBoom();
                boom.init();
                boom.attachMap(this.map);
                if (boom.initInfo(user ))
                {
                    if (CGLOBAL.g_isPlayingSound)
                    {
                        if (isNormalBoom)
                            Sound_Manager.SoundManager.getInstance().setNormalBoom.Play();
                        else
                            Sound_Manager.SoundManager.getInstance().setTimeBoom.Play();
                    }

                    boom.m_isAlive = true;


                    listBoom.Add(boom);
                }
                else
                    return;
            }
            else
            {
                if (listBoom[index].initInfo(user))
                {
                    listBoom[index].m_isAlive = true;
                }
                else
                    return;
            }
        }


        private int  isHasRoom(bool isNormalBoom )
        {
            int count = listBoom.Count;
            if (count <= 0)
                return -1;

            for (int i = 0; i < count; ++i)
            {
                if (!listBoom[i].m_isAlive)
                {
                    if (listBoom[i].Tag == FrameWork.Tag_Manager.TagManager.TAGGED.Boom && isNormalBoom)
                    {
                        return i;
                    }
                    if (listBoom[i].Tag == FrameWork.Tag_Manager.TagManager.TAGGED.ActiveBoom && !isNormalBoom)
                    {
                        return i;
                    }
                }
            }

            return -1;
        }

        public void attachMap(CTiledMap map)
        {
            this.map = new CTiledMap();
            this.map = map;
        }


        public void activeBoom(Vector2 mapPos)
        {
            int count = listBoom.Count;

            for (int i = 0; i < count; ++i)
            {
                if (listBoom[i].mapPos == mapPos && listBoom[i].m_isAlive)
                {
                    listBoom[i].explor();
                    return;
                }
            }
        }

        public void activeBoom()
        {
            int count = listBoom.Count;

            for (int i = 0; i < count; ++i)
            {
                if (listBoom[i].Tag == FrameWork.Tag_Manager.TagManager.TAGGED.ActiveBoom && listBoom[i].m_isAlive)
                {
                    listBoom[i].explor();
                }
            }
        }


        public void update(float deltaTime)
        {
            int count = listBoom.Count;

            for (int i = 0; i < count; ++i)
            {
                listBoom[i].update(deltaTime);
            }
        }

        public void draw()
        {
            int count = listBoom.Count;

            for (int i = 0; i < count; ++i)
            {
                if (listBoom[i].m_isVisible)
                {
                    listBoom[i].draw();
                }
            }
        }

        public void clear()
        {
            listBoom.Clear();
        }
    }
}
